package by.amakarevich.webapp.page.booking.info.panel;

import javax.inject.Inject;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.resource.UrlResourceReference;

import by.amakarevich.datamodel.Booking;
import by.amakarevich.datamodel.BookingInfo;
import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.ShoppingCart;
import by.amakarevich.datamodel.ShoppingCartInfo;
import by.amakarevich.datamodel.UserAccount;
import by.amakarevich.services.BookingInfoService;
import by.amakarevich.services.BookingService;
import by.amakarevich.services.ShoppingCartInfoService;
import by.amakarevich.services.ShoppingCartService;
import by.amakarevich.webapp.app.BasicAuthenticationSession;
import by.amakarevich.webapp.app.SecuredLinkForAdmin;
import by.amakarevich.webapp.page.booking.BookingPage;
import by.amakarevich.webapp.page.booking.info.BookingInfoPage;
import by.amakarevich.webapp.page.shoppingcart.ShoppingCartPage;

public class BookingInfoPanel extends Panel {

	@Inject
	private BookingInfoService bookingInfoService;
	@Inject
	private BookingService bookingService;
	
	private BookingInfo bookingInfo;
	private Booking booking;
	private Product product;

	public BookingInfoPanel(String id, BookingInfo bookingInfo, Booking booking) {
		super(id);
		this.bookingInfo = bookingInfo;
		this.booking=booking;
	}

	@Override
	protected void onInitialize() {
		super.onInitialize();
		WebMarkupContainer liContainer = new WebMarkupContainer("wrapper-li");
		add(liContainer);

		WebMarkupContainer hContaienr = new WebMarkupContainer("wrapper-h");
		liContainer.add(hContaienr);

		bookingInfo = bookingInfoService.getByBookingInfoId(bookingInfo.getId());

		product = bookingInfo.getProduct();

		hContaienr.add(new Label("item", new Model<String>(product.getItem())));
		String urlStr="http://localhost:8081/imagesService/"+product.getId().toString()+".jpg";
		final UrlResourceReference url= new UrlResourceReference(Url.parse(urlStr));
		liContainer.add(new Image("img", url));
		liContainer.add(new Label("company", new Model<String>(product.getCompany())));
		liContainer.add(new Label("quantity", new Model<Long>(bookingInfo.getQuantity())));
		liContainer.add(new Label("price", new Model<Long>(bookingInfo.getPrice())));
		
		Link<Void> securedDeleteProductLink = new SecuredLinkForAdmin("delete-product-booking-link") {

			@Override
			public void onClick() {
				if(bookingInfo.getQuantity()==1l){
					bookingInfoService.deleteBookingInfo(bookingInfo.getId());    				
					}else{
						bookingInfo.setQuantity(bookingInfo.getQuantity()-1l);
						bookingInfoService.updateBookingInfo(bookingInfo);
					}
				setResponsePage(new BookingInfoPage(booking));
			}
		};
		add(securedDeleteProductLink);
	}

}
